import {debounce, getScreen} from "../common/auth";
import pub from "../common/pub";

export default {
    data(){
        return{
            screenWidth:0,
            screenHeight:0,
			//是否结束
			end:false,
			//当前数据列表
			dataList:[],
            query:{page:1,size:20},
            total:999,
            params:{},
            keyword:'',
            triggered:false
        }
    },
    created() {
        const {width,height} = getScreen();
        this.screenHeight = height;
        this.screenWidth = width;
        const that = this
        this.query = new Proxy({
            page:1,
            size:20
        },{
            set(target, p, newValue, receiver) {
                if(p === 'page' && newValue === 1){
                    target[p] = newValue;
                    that.end = false
                    that.dataList = []
                    that.onGet()
                    return true
                }else {
                    if(!that.end){
                        const res =Reflect.set(target, p, newValue, receiver);
                        //target[p] = newValue;
                        that.onGet()
                        return res
                    }else{
                        // console.log('当前条数已最大条数'))
                         throw Error('当前条数已最大条数')
                    }
                }
            }
        })
        if(Reflect.has(this,'onGet')){
            //添加装饰器
            let _onGet = this.onGet;
            this.onGet = async function () {
                try{
                    uni.showLoading({title:'加载中'})
                    if(this.onGetBefore()){
                        await _onGet();
                        this.onGetAfter();
                    }
                    uni.hideLoading()
                }catch (e) {
                    uni.hideLoading()
                }
            }
        }
    },
    beforeDestroy(){
      //取消订阅
        pub.$remove('changeKey')
    },

    methods:{
        onPulling() {
            let that = this;
            if(!this.triggered){
                //下拉加载，先让其变true再变false才能关闭
                this.triggered = true;
                //关闭加载状态 (转动的圈)，需要一点延时才能关闭
                setTimeout(() => {
                    that.triggered = false;
                },1000)
            }
        },
        //请求之前
        onGetBefore(){
            return !this.end;
        },
        //请求之后
        onGetAfter(){
            console.log(this.dataList.length,this.total)
             if(this.dataList.length >= this.total){
                 this.end = true
             }
        },
        debounceGet:debounce(function (){
           this.query.page = 1
        })
    }
}
